<?php

namespace App\Traits;

use App\Models\RechOrder;

trait HandlePriceTrait
{
	/**
	 * 处理商家广告单的价格
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function handlePrice($id)
	{
		//查找订单
		$order = RechOrder::find($id);
		//没有订单
		if (count($order) == 0) {
			//默认价格为 0
			return 0;
		}
		//当前订单价格
		$price = $order->price;
		//最高价
		$max_price = $order->max_price;
		//最低价
		$min_price = $order->min_price;
		//溢价比例
		$spill = $order->spill;
		//判断溢价
		if ($spill != 0) {
			//百分比上限
			$per = 100 + $spill;
			//计算展示价格
			if ($per > 100) {
				$show_price = $price * (rand(100, $per)/100);
			} else {
				$show_price = $price * (rand($per, 100)/100);
			}
			
		} else {
			$show_price = $price;
		}

		//判断是否超出最大价格
		if ($show_price >= $max_price) {
			$show_price = $max_price;
		}
		//判断是否小于最低价格
		if ($show_price <= $min_price) {
			$show_price = $min_price;
		}

		return sprintf("%.2f", $show_price);

	}
}